/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
function formatImage(url, width, height, type) {

    if (url === '' || url == '10963.png') {
        url = 'empty.png';
    }

    if (url.indexOf('steamcommunity') > 0 || url.indexOf('steamchina') > 0) {
        return url;
    }

    if (url.substr(0, 4) !== 'http') {

        return url = 'http://i.c5game.com/' + url;
    }

    return url + '@' + width + 'w_' + height + 'h';
}

function formatItemWear(wear) {

    return Math.floor(wear * 10);
}

function formatAbrasion(abrasion, digit) {
    if (abrasion.length > digit) {
        // return Number(wear).toFixed(digit - 2);
        return abrasion.toString().substr(0, digit);
    } else {
        return abrasion;
    }
}

function isEmpty(val) {

    if (val == null || val == 'undefined' || val == '' || val.length == 0) {
        return true;
    } else {
        return false;
    }
}

//opt {arr:[1,2,3],start:0,end:5}
function arraySlice(opts) {
    if (typeof opts === 'object') {
        if ($.isArray(opts.arr)) {
            var start = parseInt(opts.start);
            var end = parseInt(opts.end);

            if (!isNaN(start) && !isNaN(end)) {
                return opts.arr.slice(start, end);
            }
        }
    }

    return false;
}

function goodUrl(appid, id, source) {
    var url = '',
        type = ''
    ;

    if (source == 'P' || source == 2) {
        type = 'P';
    } else {
        type = 'S';
    }

    if (appid == 570) {
        url = '/dota/' + id + '-' + type + '.html';
    } else if (appid == 730) {
        url = '/csgo/' + id + '/' + type + '.html';
    } else {
        url = '/market/' + appid + '-' + id + '-' + type + '.html';
    }

    return url;
}

function formatDate(date, fmt) {
    var date = new Date(date * 1000),
        fmt = fmt || 'yyyy-MM-dd hh:mm:ss',
        o = {
            "M+": date.getMonth() + 1,                 //月份 
            "d+": date.getDate(),                    //日 
            "h+": date.getHours(),                   //小时 
            "m+": date.getMinutes(),                 //分 
            "s+": date.getSeconds()                 //秒 
        }
    ;

    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }

    return fmt;
}

// 游戏字体图标
function gameIcon(appid) {
    var asGameFont = {
            "570": "&#xe815;",  //DOTA2
            "730": "&#xe816;",  //CS:GO
            "433850": "&#xe817;",   //H1Z1
            "440": "&#xe81b;",  //军团要塞2(TF2)
            "753": "&#xe819;",  //Steam
            "218620": "&#xe81c;",   //收获日2(Payday2)
            "578080": "&#xe818;",   //绝地求生(PUBG) 
            "295110": "&#xe81a;",   //Just Survive
            "360930": "&#xe6f2;",   //KF2
            "304930": "&#xe6f1;",   //Unturned
            "252490": "&#xe6f0;"  //Rust
        },
        sCurGameFont = ""
    ;

    for (var key in asGameFont) {
        if (key == appid) {
            sCurGameFont = asGameFont[key];
        }
    }

    return sCurGameFont;
}

function tradableTimesteam(seconds) {

    let h = Math.floor(seconds / 3600)
    let m = Math.floor((seconds % (3600 * 24) % 3600) / 60)
    let str = ''
    if (h) {
        if (h > 24) {
            let y = Math.floor(seconds / 3600 / 24)
            str = y + "天" + h % 24 + "小时"
        } else {
            str = h + "小时"
        }
    } else {
        str = m + "分钟"
    }
    return str
}

template.helper('image', formatImage);
template.helper('wear', formatItemWear);
template.helper('abrasion', formatAbrasion);
template.helper('isEmpty', isEmpty);
template.helper('arraySlice', arraySlice);
template.helper('goodUrl', goodUrl);
template.helper('formatDate', formatDate);
template.helper('gameIcon', gameIcon);
template.helper('tradableTimesteam', tradableTimesteam);
